<?php

namespace App\Models\Panx;

use App\Models\Customer\Language;
use App\Services\Utils\Guard;
use App\Services\Utils\Memorable;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;

/**
 * Class CategoryDescription
 *
 * @mixin \Illuminate\Database\Eloquent\Builder
 * @mixin \Illuminate\Database\Query\Builder
 *
 * @package App\Models\Panx
 */
class CategoryDescription extends Model
{
    use Memorable;

    /**
     * @var Collection|self[]
     */
    public static $all_cache;

    protected $table = 'category_descriptions';
    protected $guarded = [];

    /**
     * @param int $category_id
     * @return string|null
     */
    public static function mapIDtoName(int $category_id): ?string
    {
        $language_code = app()->getLocale();
        $language_id = Language::mapCodeToId($language_code);
        self::$all_cache = (self::$all_cache ?: (new self)->where('language_id', $language_id)->get());

        return self::singletonMemorize(function () use ($category_id) {
            /** @var CategoryDescription $self */
            $self = self::$all_cache->where('category_id', $category_id)->first();

            return $self->name ?? null;
        })();
    }
}
